What’s New in Service Broker
With the release of SQL Server 2008, Service Broker includes the following new features:
A new diagnostic command-line utility called ssbdiagnose
Prioritization of conversations
Additional performance counters (for use with System Monitor)
Enhanced configurability via SQL Server Management Studio (SSMS)
Although in versions of SQL
Server prior to SQL Server 2005, it was possible to create structures
analogous to some of the new database objects used with Service Broker
(for example, using a table as a work queue), such implementations do
not even come close to providing the reliability, ease of use, and
scalability of Service Broker. This article introduces SQL Server’s
native messaging-oriented framework.
Understanding Distributed Messaging
If you have experience with
Microsoft Message Queuing (MSMQ) or IBM’s MQSeries, you already know the
paradigm in play: two or more distinct database-driven applications
reside on one or more servers, yet they need to collaborate, acting as a
single unit to successfully complete a set of tasks. These applications
may have varying implementations, but they are still considered to be
part of the same distributed
system. The constraints on these systems are such that the applications
involved must be able to communicate in the freest, most reliable way
possible.
Free,
in this context, means that the applications cannot make synchronous
method calls (or even asynchronous callbacks) to each other; they must
be able to send or receive messages without having to wait for the other
to reply or acknowledge. Such applications are said to be loosely coupled.
Reliable,
in this context, means that even if one partner in the collaboration
isn’t up and running when the other needs to send or receive a message,
they must still be able to do so in such a way that the message will be
stored for later processing, with its integrity and send order
guaranteed.
For any of this to happen, a
basic infrastructure must be in place that such applications can rely
on—one that is independent of and yet enables such communications. This
is Service Broker’s reason for being.
The Basics of Service Broker
A default instance of
Service Broker exists for every database you create. To find it by using
the SQL Server Management Studio Object Browser, you look under the Service Broker node directly under your database’s root node.
Note
When you expand the Services node under the Service Broker
node, it becomes readily apparent that, under the covers, SQL Server
uses Service Broker services to implement some of its other built-in
functionality, including Database Mail.
If you expand a few subnodes under the Service Broker node, you can see most of the new Service Broker constructs:
Messages— These (optionally typed) envelopes contain the data to be interchanged.
Contracts— These rules define the flow of messages (that is, which message types can flow from service to service).
Queues— These are storage facilities for messages.
Services— Services are endpoints in a Service Broker application that send and/or receive messages.
Routes— These are associations between network addresses and services running on remote machines.
Remote service bindings—
These are associations between database principals and remote services
for authorization and message encryption (using certificates).
A few constructs are not directly named in the subnodes:
Conversations— These are communications between two or more services.
Dialogs— These are conversations between exactly two services.
Conversation groups— A conversation group consists of one or more related conversations.
The sections that follow
cover all these objects in detail. But before you get to that, you need
to set up the entities required to execute this article’s examples. The
first step is to configure a new database (as well as AdventureWorks2008)
to work with Service Broker. Note that Service Broker is turned off by
default. You configure Service Broker at the database level by using a
few new Data Definition Language (DDL) options:
DISABLE_BROKER— This option turns off Service Broker for a database.
ENABLE_BROKER— This option turns on Service Broker for the database. (You can check to see whether Service Broker is enabled by querying the is_broker_enabled column in sys.databases.)
ERROR_BROKER_CONVERSATIONS—
This option turns on Service Broker and drops an error message in all
queues for active conversations when the database is attached, giving
Service Broker services a chance to respond to this special message and
gracefully self-terminate.
NEW_BROKER— This option changes the unique identifier for Service Broker in the database instance (that is, the value of service_broker_guid in sys.databases),
effectively terminating any running conversations with an error
message. This is essentially a plug-pulling mechanism. Note that each
Service Broker instance gets its own unique identifier, which Service
Broker services use for inter-database messaging .
Note that you can set these options only by using CREATE DATABASE when performing an attach; therefore, you need to create the database as you normally would and then use ALTER DATABASE, providing one of the options just described. To turn on Service Broker for AdventureWorks2008, you execute the following:
USE Master
GO
ALTER DATABASE AdventureWorks2008 SET ENABLE_BROKER
You also need to set the TRUSTWORTHY option to ON (it is OFF by default) for databases involved with messaging and to create an encrypted MASTER KEY for every database:
USE Master
GO
ALTER DATABASE AdventureWorks2008 SET TRUSTWORTHY ON
GO
USE AdventureWorks2008
GO
-- Note: Password validation using a password as simple as the following
-- will fail when Group Policy demands strong validation.
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = 'AdventureWorks2008';
You need to create a sample database that resides on the same SQL Server 2008 instance as AdventureWorks2008.
The concept behind this example is that a bicycle manufacturer and
seller (AdventureWorks Cycles) must provide new and updated parts data
to a sister company (Extreme Catalog Management, a.k.a. XCatMgmt) that
implements a leading catalog management system on the same network.
AdventureWorks Cycles must keep XCatMgmt up-to-date with its product
model data; otherwise, it could lose market share or end up receiving
orders from distributors based on out-of-date catalog information. The
reason is that the AdventureWorks Cycles products are featured in
several of XCatMgmt’s publications, which industry resellers use to
choose what to buy. Listing 1 shows how you create the entities for the XCatMgmt sample database.
Listing 1. DDL for a Service Broker–Enabled Simplified Product Catalog Database
CREATE DATABASE XCatMgmt WITH TRUSTWORTHY ON GO ALTER DATABASE XCatMgmt SET ENABLE_BROKER GO USE XCatMgmt GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'XCatMgmt'; GO CREATE SCHEMA Publication CREATE TABLE Publication.BaseCatalog ( CatalogId int IDENTITY(1,1) PRIMARY KEY, CatalogName varchar(100) NOT NULL, LastPublicationDate datetime ) CREATE TABLE Publication.Product ( ProductId int IDENTITY(1,1) PRIMARY KEY, ProductName varchar(100) NOT NULL, SourceProductId int NOT NULL, ManufacturerId int NOT NULL, Price money NOT NULL, ProductDetailXml xml, CreateDate datetime NOT NULL, LastUpdateDate datetime DEFAULT GETDATE() ) CREATE TABLE Publication.ProductCatalogType ( ProductCatalogTypeId int PRIMARY KEY, Description varchar(100) ) CREATE TABLE Publication.ProductCatalog ( ProductCatalogId int IDENTITY(1,1) PRIMARY KEY, ProductCatalogTypeId int NOT NULL REFERENCES Publication.ProductCatalogType(ProductCatalogTypeId), ProductId int NOT NULL REFERENCES Publication.Product(ProductId), CatalogId int NOT NULL REFERENCES Publication.BaseCatalog(CatalogId) ) GO INSERT Publication.ProductCatalogType SELECT 1, 'Mountain Bicycles and Parts' INSERT Publication.BaseCatalog (CatalogName) SELECT 'The Complete Catalog of Wholesale Mountain Bike Parts' INSERT Publication.Product ( ProductName, SourceProductId, ManufacturerId, Price, ProductDetailXml, CreateDate ) SELECT 'AdventureWorksProductName', 749, 1, 99.99, '<empty/>', GETDATE() INSERT Publication.ProductCatalog SELECT 1, 1, 1
|